package edu.gdkm.ssm.service.impl;

import edu.gdkm.ssm.dao.IRoleDao;
import edu.gdkm.ssm.domain.Permission;
import edu.gdkm.ssm.domain.Role;
import edu.gdkm.ssm.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements IRoleService {

    @Autowired
    private IRoleDao roleDao;

    @Override
    public List<Role> findAll() {
        return this.roleDao.findAll();
    }

    @Override
    public void save(Role role) {
        this.roleDao.save(role);
    }

    @Override
    public List<Role> findOtherRole(String id) {
        return this.roleDao.findOtherRoles(id);
    }

    @Override
    public Role findById(String roleId) {
        return this.roleDao.findById(roleId);
    }

    @Override
    public List<Permission> findOtherPermissions(String roleId) {
        return this.roleDao.findOtherPermissions(roleId);
    }

    @Override
    public void addPermissionToRole(String roleId, String[] permissionIds) {
        for(String permissionId : permissionIds){
            this.roleDao.addPermissionToRole(roleId, permissionId);
        }
    }

    @Override
    public void deleteRoleById(String roleId) {
        // 从user_role表中删除
        roleDao.deleteFromUser_RoleByRoleId(roleId);
        // 从role_permission表中删除
        roleDao.deleteFromRole_PermissionByRoleId(roleId);
        // 从role表中删除
        roleDao.deleteRoleById(roleId);
    }
}
